package fr.paulmasson.jfreex.db.dao;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import fr.paulmasson.jfreex.db.DbManager;
import fr.paulmasson.jfreex.db.bo.BoChannel;
import fr.paulmasson.jfreex.db.bo.BoProvider;
import fr.paulmasson.jfreex.utils.Common;
import fr.paulmasson.jfreex.utils.Wget;

public class DaoProvider {

	public static void setup() {
		if (isEmpty()) {
			try {
				String data = Wget.wget(Common.providersUrl);
				String[] data_lines = data.split("\n");
			
				for (String line : data_lines) {
					if (line.length() > 0) {
						String[] infos = line.split("	");
						BoProvider prov = new BoProvider();
						prov.setName(infos[0]);
						prov.setChannelUrl(infos[1]);
						prov.setChannelUrlMirror(infos[2]);
						DbManager.saveEntity(prov);
					}
				}
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static int count() {
		return DbManager.get("from BoProvider").size();
	}
	
	public static boolean isEmpty() {
		return count()==0;
	}

	public static BoProvider getBoProvider(int id) {
		List<Object> list = DbManager.get("from BoProvider where id="+id+"");
		if (list.size()==1)
			return (BoProvider) list.get(0);
		else
			return null;
	}
	
	
	public static void retrieveChannel(BoProvider prov, boolean mirror) {
		String url = prov.getChannelUrl();
		if (mirror) {
			url = prov.getChannelUrlMirror();
		}
		
		
		
		try {
			String playlist = Wget.wget(url);
			String[] lines = playlist.split("\n");
			
			
			String old_channel = "";
			
			boolean hd,ld;
			

			BoChannel c = new BoChannel();
			
			for (int i=0; i<lines.length; i++) {
				
				String line = lines[i];
				
				if (!line.equals("#EXTM3U")) {
					
					if (line.substring(0, 7).equals("#EXTINF")) { 
						
						
						String new_channel = line.split(" - ")[1];
						String hd_new_channel = new_channel.replaceAll(" HD", "");
						String ld_new_channel = new_channel.replaceAll(" \\(bas débit\\)", "");
						
						hd = hd_new_channel.equals(new_channel);
						ld = ld_new_channel.equals(new_channel);
						
						String real_channel = new_channel.replaceAll(" +$", "").replaceAll(" HD", "").replaceAll(" \\(bas débit\\)", "");
						
						
						String channel_url = lines[++i];
					
						if (!old_channel.equals(real_channel)) {
							if (!c.getName().isEmpty()) {
								prov.getChannels().add(c);
							}
							c = new BoChannel();
						}
						
						if (!hd) 
							c.setHd(channel_url);
						else if (!ld)
							c.setLd(channel_url);
						else
							c.setSd(channel_url);
						c.setProvider(prov);
						c.setName(real_channel);
						
						old_channel = real_channel;
						
					} else if (line.substring(0, 10).equals("#EXTVLCOPT")) {
						break;
					}
				}
			}
			
			DbManager.updateEntity(prov);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
	
	
	public static void printTable(List<Object> data) {
		Dao.printTable(data);
	}

	public static String toFormattedString() {
		List<Object> list = DbManager.get("from BoProvider");
		list.add(0,new BoProvider());
		printTable(list);
		return "";
	}
	
}
